﻿angular.module('picShare').controller('currentAlbumController', function ($scope, $rootScope, inMemoryDataSvc, pageLocatorSvc, imageDataSvc, getImagesSvc, safeApply, httpRequestSvc, userDataSvc) {
    $scope.title = "Current Album Page";
    console.log("from controller");
    console.log(imageDataSvc.imageCounter);
    $scope.backToAlbumsPage = function () {
        imageDataSvc.imageData = [];
        pageLocatorSvc.goTo("/albums");
    };

    $scope.$watch(function () {
        return imageDataSvc.imageData.length;
    }, function (newVal) {
        $scope.photos = imageDataSvc.imageData;
    });

    //    var counter = 4;
    //    var items = 4;

    // initial image index
    $scope._Index = 0;

    // if a current image is the same as requested image
    $scope.isActive = function (index) {
        return $scope._Index === index;
    };

    // show prev image
    $scope.showPrev = function () {
        if ($scope._Index > 0) {
            --$scope._Index;
        }
        else {
            $scope._Index = $scope.photos.length - 1;

        }
    };

    // show next image
    $scope.showNext = function () {
        console.log("len");
        console.log($scope.photos.length);
        if ($scope._Index < $scope.photos.length - 1) {
            $scope._Index++;
            //console.log($scope._Index);
        }
        else {
            if (imageDataSvc.imageCounter < 0) {
                imageDataSvc.imageCounter = imageDataSvc.items;
                return;
            }
            if (imageDataSvc.imageCounter >= imageDataSvc.currentAlbumLength) {
                imageDataSvc.imageCounter = 0;
                console.log("In check");
            }
            console.log("counter from slider " + imageDataSvc.imageCounter);
            imageDataSvc.imageData = [];
            getImagesSvc.getImages(imageDataSvc.imageCounter, imageDataSvc.items);
            $scope.photos = imageDataSvc.imageData;
            imageDataSvc.imageCounter = imageDataSvc.imageCounter + imageDataSvc.items;
            $scope._Index = 0;
        }
    };

    // show a certain image
    $scope.showPhoto = function (index) {
        $scope._Index = index;
    };
});